﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Forms;

namespace FollowTheMouseWPF
{
	/// <summary>
	/// Interaction logic for EyeControl.xaml
	/// </summary>
	public partial class EyeControl : System.Windows.Controls.UserControl
	{
		public EyeControl()
		{
			this.InitializeComponent();

            this.Loaded += (s, e) =>
                {
                    _helper.Visibility = System.Windows.Visibility.Collapsed;
                };

            this.SizeChanged += (s, e) =>
                {
                    Lense.Width = ActualWidth;
                    Lense.Height = ActualHeight;

                    double left = ActualWidth / 2 - Iris.ActualWidth / 2;
                    double top = ActualHeight / 2 - Iris.ActualHeight / 2;

                    Iris.SetValue(Canvas.LeftProperty, left);
                    Iris.SetValue(Canvas.TopProperty, top);
                };
		}

        public void SetMouse(Point mouse)
        {
            System.Drawing.Rectangle screen = SystemInformation.VirtualScreen;
            
            Point locationOnScreen = this.PointToScreen(new Point(0, 0));

            double relativeX;
            double relativeY;

            double distanceX = -1 * (locationOnScreen.X + ActualWidth / 2 - mouse.X);
            double distanceY = -1 * (locationOnScreen.Y + ActualHeight / 2 - mouse.Y);

            if (distanceX < 0)
            {
                relativeX = -1 * (distanceX + ActualWidth / 2) / (locationOnScreen.X);
            }
            else
            {
                relativeX = (distanceX - ActualWidth / 2)/ (screen.Width - locationOnScreen.X);
            }

            if (distanceY < 0)
            {
                relativeY = -1 * (distanceY + ActualHeight / 2 )/ (locationOnScreen.Y);
            }
            else
            {
                relativeY = (distanceY - ActualHeight / 2) / (screen.Height - locationOnScreen.Y);
            }

            double angle = Math.Atan2(distanceX, distanceY);
            double distance = Math.Sqrt(distanceX * distanceX + distanceY * distanceY);

            double angleX = (Math.Sin(angle));
            double angleY = (Math.Cos(angle));

            double left = ActualWidth / 2 + ActualWidth / 2 * angleX * relativeX;
            double top = ActualHeight / 2 + ActualHeight / 2 * angleY * relativeY;

            _helper.Text = "distancex: " + distanceX + Environment.NewLine
                + "distancey: " + distanceY + Environment.NewLine
                + "angle: " + angle + Environment.NewLine
                + "distance: " + distance + Environment.NewLine
                + "left: " + left + Environment.NewLine
                + "top: " + top + Environment.NewLine
                + "relativeX: " + relativeX + Environment.NewLine 
                + "relativeY: " + relativeY + Environment.NewLine 
                + "angleX: " + angleX + Environment.NewLine
                + "angleY: " + angleY + Environment.NewLine;

            Iris.SetValue(Canvas.LeftProperty, left);
            Iris.SetValue(Canvas.TopProperty, top);
        }
	}
}